package com.karaoke.android.home.fragment;

import android.app.DialogFragment;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

/**
 * Created by allen on 2017/4/25.
 */

public abstract class QuickActionDialogFragment extends DialogFragment {

    public static final int ALIGN_SCREEN_LEFT = 1;
    public static final int ALIGN_SCREEN_RIGHT = 2;
    public static final int ALIGN_SCREEN_TOP = 4;
    public static final int ALIGN_SCREEN_BOTTOM = 8;
    public static final int ALIGN_ANCHOR_VIEW_LEFT = 16;
    public static final int ALIGN_ANCHOR_VIEW_RIGHT = 32;
    public static final int ALIGN_ANCHOR_VIEW_TOP = 64;
    public static final int ALIGN_ANCHOR_VIEW_BOTTOM = 128;

    protected ImageView ivArrow;
    private View mAnchorView;
    private int mFlags = 129;
    private int mScreenWidth = 720;
    private int mScreenHeight = 1200;
    private int mAnchorWidth;
    private int mAnchorHeight;
    private int[] mAnchorLocation = new int[2];
    private int mGravity = 0;
    private static final int DEFAULT_STATUS_BAR = 24;
    private static final String TAG = "QuickActionDialogFragment";

    public QuickActionDialogFragment() {
    }

    public QuickActionDialogFragment setAligmentFlags(int flags) {
        if (flags != 0) {
            this.mFlags = flags;
        }
        return this;
    }

    public QuickActionDialogFragment setAnchorView(View anchorView) {
        this.mAnchorView = anchorView;
        return this;
    }

    protected abstract int getArrowImageViewId();

    protected abstract int getLayout();

    protected boolean isCanceledOnTouchOutside() {
        return true;
    }

    protected boolean isStatusBarVisible() {
        return false;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(this.getLayout(), container, false);
        if (this.getArrowImageViewId() != 0) {
            this.ivArrow = (ImageView) view.findViewById(this.getArrowImageViewId());
        }

        this.getScreenSize();
        this.getAnchorViewSizeAndPosition();
        this.setupDialogPosition();
        this.setupImageIcon(view);
        return view;
    }

    protected void setupDialogPosition() {
        this.getDialog().setCanceledOnTouchOutside(this.isCanceledOnTouchOutside());
        WindowManager.LayoutParams p = this.getDialog().getWindow().getAttributes();
        p.width = -1;
        p.height = -1;
        p.y = 0;
        this.alignBaseOnFlags(p);
        this.getDialog().getWindow().setGravity(this.mGravity);
        this.getDialog().getWindow().setAttributes(p);
    }

    private void alignBaseOnFlags(WindowManager.LayoutParams params) {
        if ((this.mFlags & 1) == 1) {
            this.alignScreenLeft(params);
        }

        if ((this.mFlags & 2) == 2) {
            this.alignScreenRight(params);
        }

        if ((this.mFlags & 4) == 4) {
            this.alignScreenTop(params);
        }

        if ((this.mFlags & 8) == 8) {
            this.alignScreenBottom(params);
        }

        if ((this.mFlags & 16) == 16) {
            this.alignAnchorViewLeft(params);
        }

        if ((this.mFlags & 32) == 32) {
            this.alignAnchorViewRight(params);
        }

        if ((this.mFlags & 64) == 64) {
            this.alignAnchorViewTop(params);
        }

        if ((this.mFlags & 128) == 128) {
            this.alignAnchorViewBottom(params);
        }

    }

    private void alignScreenLeft(WindowManager.LayoutParams params) {
        this.mGravity |= 3;
        params.x = 0;
    }

    private void alignScreenRight(WindowManager.LayoutParams params) {
        this.mGravity |= 5;
        params.x = 0;
    }

    private void alignScreenTop(WindowManager.LayoutParams params) {
        this.mGravity |= 48;
        params.y = 0;
    }

    private void alignScreenBottom(WindowManager.LayoutParams params) {
        this.mGravity |= 80;
        params.y = 0;
    }

    private void alignAnchorViewLeft(WindowManager.LayoutParams params) {
        this.mGravity |= 3;
        params.x = this.mAnchorLocation[0];
    }

    private void alignAnchorViewRight(WindowManager.LayoutParams params) {
        this.mGravity |= 5;
        params.x = this.mScreenWidth - this.mAnchorLocation[0] - this.mAnchorWidth;
    }

    private void alignAnchorViewTop(WindowManager.LayoutParams params) {
        this.mGravity |= 48;
        params.y = this.mAnchorLocation[1];
        if (this.isStatusBarVisible()) {
            params.y -= this.getStatusBarHeight();
        }

    }

    private void alignAnchorViewBottom(WindowManager.LayoutParams params) {
        this.mGravity |= 48;
        params.y = this.mAnchorLocation[1] + this.mAnchorHeight;
        if (this.isStatusBarVisible()) {
            params.y -= this.getStatusBarHeight();
        }

    }

    private void setupImageIcon(final View dialogView) {
        if (this.ivArrow != null) {
            final android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) this.ivArrow.getLayoutParams();
            this.ivArrow.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                    QuickActionDialogFragment.this.ivArrow.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    int[] dialogLocation = new int[2];
                    dialogView.measure(0, 0);
                    int dialogWidth = dialogView.getWidth();
                    dialogView.getLocationOnScreen(dialogLocation);
                    QuickActionDialogFragment.this.ivArrow.measure(0, 0);
                    int arrowWidth = QuickActionDialogFragment.this.ivArrow.getWidth();
                    int maxMarginLeft = dialogWidth - arrowWidth;
                    byte minMarginLeft = 0;
                    if (dialogLocation[0] + dialogWidth < QuickActionDialogFragment.this.mAnchorLocation[0]) {
                        params.leftMargin = maxMarginLeft;
                    } else if (QuickActionDialogFragment.this.mAnchorLocation[0] + QuickActionDialogFragment.this.mAnchorWidth <= dialogLocation[0]) {
                        params.leftMargin = minMarginLeft;
                    } else {
                        int margin = QuickActionDialogFragment.this.mAnchorLocation[0] + QuickActionDialogFragment.this.mAnchorWidth / 2 - dialogLocation[0] - arrowWidth / 2;
                        if (margin < minMarginLeft) {
                            margin = minMarginLeft;
                        }

                        if (margin > maxMarginLeft) {
                            margin = maxMarginLeft;
                        }

                        params.leftMargin = margin;
                    }

                }
            });
        }
    }

    private int getStatusBarHeight() {
        if (this.getActivity() == null) {
            DisplayMetrics rectangle1 = this.getResources().getDisplayMetrics();
            return (int) (rectangle1.density * 24.0F);
        } else {
            Rect rectangle = new Rect();
            Window window = this.getActivity().getWindow();
            window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
            int statusBarHeight = rectangle.top;
            return statusBarHeight;
        }
    }

    private void getAnchorViewSizeAndPosition() {
        if (this.mAnchorView == null) {
            throw new IllegalStateException("AnchorView not found! You must set AnchorView first");
        } else {
            this.mAnchorView.getLocationInWindow(this.mAnchorLocation);
            this.mAnchorView.measure(0, 0);
            this.mAnchorWidth = this.mAnchorView.getWidth();
            this.mAnchorHeight = this.mAnchorView.getHeight();
        }
    }

    private void getScreenSize() {
        if (this.getActivity() != null) {
            DisplayMetrics displaymetrics = new DisplayMetrics();
            this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            this.mScreenHeight = displaymetrics.heightPixels;
            this.mScreenWidth = displaymetrics.widthPixels;
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setStyle(2, 0);
    }
}
